package coms.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import com.beans.AdminUserBean;
import com.beans.CategoryBean;
import com.beans.CompanyBean;
import com.beans.FormBean;
import com.beans.ORGBean;
import com.beans.PayWayBean;
import com.crc.mam.BaseCase;
import coms.AutoException;
import coms.Constants;
import coms.TestDateString;
import frame.FrameWork;
import frame.InterTest;
import functions.ems_base_data.Category;
import functions.ems_base_data.MdBank;
import functions.ems_base_data.MdOu;
import functions.ems_claim.Claim;
import functions.ems_claim.ClaimProcess;
import functions.ems_claim.Payment;
import functions.ems_process_service.Psc;
import functions.mams_rest.Org;
import functions.mams_rest.Sys;
import functions.mams_rest.User;

/**
 * 发起报账申请--》付款准备--》保证金退款--》其他保证金退款----审批
 * @author CHENHAILIAN6
 *
 */
public class PayClaimProcess extends BaseCase implements InterTest {
  private org.slf4j.Logger logger = LoggerFactory.getLogger(PayClaimProcess.class);
    String claimId;
    String claimNo;
    String amount ;
    String categoryCode ;
    String categoryName;
    String applyCompany;
    ORGBean applyOrgBean;
    
	public void setCategoryName(String categoryName) {
      this.categoryName = categoryName;
    }

  public String getCategoryCode() {
      return categoryCode;
    }

    public void setCategoryCode(String categoryCode) {
      this.categoryCode = categoryCode;
    }

  public String getAmount() {
      return amount;
    }

    public void setAmount(String amount) {
      this.amount = amount;
    }


	public String getClaimId() {
		return claimId;
	}

	public void setClaimId(String claimId) {
		this.claimId = claimId;
	}

	public String getClaimNo() {
		return claimNo;
	}

	public void setClaimNo(String claimNo) {
		this.claimNo = claimNo;
	}

	public String getApplyCompany() {
    return applyCompany;
  }

  public void setApplyCompany(String applyCompany) {
    this.applyCompany = applyCompany;
  }

  public ORGBean getApplyOrgBean() {
    return applyOrgBean;
  }

  public void setApplyOrgBean(ORGBean applyOrg) {
    this.applyOrgBean = applyOrg;
  }



	@Override
	public void run(Map<Object, Object> paramMap) {
	   // TODO Auto-generated method stub
      logger.info(" 创建付款申请------------start---------------------");
      String baseUrl = (String) paramMap.get("baseUrl");
      if(baseUrl !=null) {
        FrameWork.setBaseUrl(baseUrl);
      }

       LoginTest loginTest = new LoginTest();
       loginTest.run(paramMap);
       if(!loginTest.status) {
         remark = loginTest.result;
         status = false;
         throw new AutoException("登录失败，阻碍测试");
       }
      
        CategoryBean categoryBean = null ;
        categoryCode = (String) paramMap.get("categoryCode");
        categoryName = (String) paramMap.get("categoryName");
        if(categoryCode == null) {
          Category category = new Category();
          categoryBean = category.getCategoryByName(categoryName);
          categoryCode = categoryBean.getCategoryCode();
          if(categoryBean == null) {
            throw new AutoException("找不到对应的业务大类：" +categoryName+ ", 请确认后重新提交");
          }
        }
        
        User user = new User();
        user.admin();
        AdminUserBean userBean = user.getUserBean();
        MdOu ou = new MdOu(userBean);
        ou.getCompanyByCompanyName(userBean.getCompanyDesc());
        CompanyBean myCompany =  ou.getMyCompany();
        Org org = new Org();
        applyCompany = userBean.getCompanyDesc();
        applyOrgBean = org.getRandomOrg(applyCompany, userBean.getDepartment());

        Map<Object,Object> map2 = new HashMap<Object,Object>();
        map2.put("currentPage", "1");
        map2.put("pageSize", "10");
        map2.put("ouCode", myCompany.getCompanycode());
        map2.put("payPlandueDateFrom", "2016-03-01");
        map2.put("payPlandueDateTo", TestDateString.today());
        /**
         * 查询付款计划行
         */
        Payment payment = new Payment();
        JSONObject object = payment.getVisibelPlan(map2);   
        this.amount = String.valueOf(object.getFloat("invoiceAmount"));
        logger.info("invoice Amount : " + amount);
        if(payment.status) {        
               
                JSONArray array = new JSONArray();
                array.put( object);
                paramMap.put("payLineJsonData", array);
                //System.out.println("payLineJsonData-----" + array.toString());
                /**
                 * 创建付款申请
                 */
                Claim claim = new Claim();
                claim.payRequstSaveClaimHeadAndLine(paramMap);//创建付款申请              
                if(claim.status) {
                    this.claimId = claim.getClaimId();
                    System.out.println(claim.getClaimId());     
                  //  JSONObject requestResultJSON  = claim.getResultObject();
                  //  JSONArray payRequestLines = requestResultJSON.getJSONArray("payRequestLines");
                    Psc psc = new Psc();
                    psc.selectFlowParam(categoryCode);
                    FormBean formBean = psc.getForm();
                    /**
                     * 保存报账单头信息
                     */
                 //   paramMap.put("payRequestLines", payRequestLines);
                    paramMap.put("claimId", claimId);
                    claim.payClaimInfoSaveClaimHeadAndLine(paramMap);
                    if(!claim.status) {
                      status = false;
                      remark = claim.getResult();
                      throw new AutoException("表单头保存失败，无法继续进行测试");
                    }
                    paramMap.put("payWay", "3");
                    Sys sys = new Sys();
                    PayWayBean payWayBean = sys.payWay(paramMap);
          
                    /**
                     * 查询银行信息
                     */
             //       MdBank bank = new MdBank();
                    paramMap.put("compCode", myCompany.getCompanycode());
                //    JSONArray banks = bank.findMdBankListByCondition(paramMap);
                    ClaimProcess process = new ClaimProcess();
                    process.submitProcess(claimId, formBean);
                    if(!process.status) {
                      status = false;
                      remark = process.remark;
                     
                    }else {
                     // claimNo = process.getClaimNo();
                      status = true;
                      remark = "[claimId="+claimId+",claimNum="+claimNo+",amount="+amount+"]";
                    }                    
                }
            
        }
        logger.info(" 创建付款申请------------end---------------------");
	}

  public String getCategoryName() {
    // TODO Auto-generated method stub
    return remark;
  }

  @Override
  public boolean getStatus() {
    // TODO Auto-generated method stub
    return status;
  }

}
